package com.rfsp.mcp.controller.base;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rfsp.common.base.ResultVO;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.FileUtil;
import com.rfsp.mcp.pojo.dto.FinanceDTO;
import com.rfsp.mcp.pojo.po.AuthPO;
import com.rfsp.common.pojo.po.UserPO;
import com.rfsp.mcp.pojo.vo.FinanceVO;
import com.rfsp.mcp.service.AuthService;
import com.rfsp.mcp.service.FinanceService;
import com.rfsp.common.service.OrgService;

@Controller("BaseFinanceController")
@RequestMapping("/base/finance")
public class BaseFinanceController extends BaseController {
    @Autowired
    FinanceService service;
    
    @Autowired
    AuthService authService;

    @Autowired
    OrgService orgService;
    
    /**上传地址*/
    @Value("${mcp.file-pdf-path-finance}")
    private String filePDFPath;
    
    /**上传地址*/
    @Value("${mcp.file-temp-path}")
    private String fileTempPath;

    @GetMapping("/detail")
    public String detail(ModelMap model,Long id,String aid) {
        FinanceVO financeVO=service.getById(id);
        if(!financeVO.getAid().equals(aid)){
            return this.errorAidNotMatch(model);
        }
        service.combo(financeVO);

        //变为已查看
        if("1".equals(financeVO.getFinanceViewStatus())){
            UserPO currentUser = ShiroUtils.getCurrentUser();
            if(currentUser.getOrgId().equals(financeVO.getBankOrgId())){

                FinanceDTO dto=new FinanceDTO();
                dto.setOptUserId(currentUser.getId());
                service.updateToViewed(id,dto);
            }
        }

        financeVO.setFilePath("/mcp/base/finance/download/?id="+financeVO.getId());
        
        ResultVO<FinanceVO> result = ResultVO.success(financeVO);
        model.put("vo", result);
        
        
        return "base/finance/detail";
    }
    
    @RequestMapping("/download")
    public String download(HttpServletRequest request, HttpServletResponse response, ModelMap model,Long id) {
    	
    	FinanceVO financeVO = service.getById(id);
    	
        try {
			FileUtil.downloadfile(request, response, financeVO.getFilePath(), financeVO.getFinanceNo()+".pdf");
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        return null;
    }
    
    
    @RequestMapping("/download3in1")
    public String download3in1(HttpServletRequest request, HttpServletResponse response, ModelMap model,Long id) {
    	
    	FinanceVO financeVO = service.getById(id);
    	AuthPO authPO = authService.get(financeVO.getAuthId());
    	
        try {
			List<File> files = new ArrayList<File>();
			files.add(new File(financeVO.getFilePath()));
			files.add(new File(authPO.getFilePath()));
			
			FileUtil.downLoadFiles(files, financeVO.getFinanceNo()+".rar", request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        return null;
    }
    

    @GetMapping("/pdf")
    public String pdf(ModelMap model,Long id) {
        return "base/finance/detail";
    }
}